package com.ecar.api.service.common.impl;

import java.text.MessageFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ecar.api.service.common.NotifyService;
import com.ecar.api.support.util.Props;
import com.ecar.api.support.util.Redis;
import com.ecar.common.cipher.MD5;
import com.ecar.common.util.HttpClientUtil;
import com.ecar.common.util.StringUtil;
@Service
@Transactional
public class NotifyServiceImpl implements NotifyService{
	
	protected static final Logger log = LoggerFactory.getLogger(NotifyServiceImpl.class);
	
	@Override
	public boolean sendCaptcha(String phone, String content) {
		if (StringUtil.isEmpty(phone) || StringUtil.isEmpty(content)) return false;

		boolean success = false;
		try {
			//30大神用的配置文件在数据库里查的，这里为了简便，所以写进配置文件里
			/*String url = (String) ConfigStore.getValue(Configs.SMS_URL);
			String key = (String) ConfigStore.getValue(Configs.SMS_KEY);
			String uname = (String) ConfigStore.getValue(Configs.SMS_UNAME);*/
			//从配置文件 config.properties 里读入；
			//这三个是URL，密码，用户名
			String url=Props.getValue("Configs.SMS.URL");
			String key=Props.getValue("Configs.SMS.KEY");
			String uname=Props.getValue("Configs.SMS.UNAME");
			
			//使用MD5解密
			key = new MD5().getMD5ofStr(key).toUpperCase();
			log.info("给 {} 发短信，短信内容：{}", phone, content);
			String param =
				"<Group Login_Name=\"{0}\" Login_Pwd=\"{1}\" OpKind=\"0\" InterFaceID=\"\" SerType=\"smit\">{2}</Group>";
			String item = "<E_Time></E_Time>"
				+ "<Item><Task>"
				+ "<Recive_Phone_Number>{0}</Recive_Phone_Number>"
				+ "<Content><![CDATA[{1}]]></Content>"
				+ "<Search_ID>{2}</Search_ID>"
				+ "</Task></Item>";

			item = MessageFormat.format(item, phone, content, phone);
			param = MessageFormat.format(param, uname, key, item);
			String responseString = HttpClientUtil.httpPostContent(url, param, "gbk", "application/xml");
			if ("00".equals(responseString)) {
				success = true;
				//短信发送成功
				Redis.c.incr(Redis.K.SMS_SUCCESS.me());
				log.info("{}，短信发送成功！", phone);
			} else {
				success = false;
				//短信发送失败
				Redis.c.incr(Redis.K.SMS_ERROR.me());
				log.error("{}，短信发送失败：{}", phone, responseString);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return success;
	}
	
	@Override
	public boolean sendSMS(String phones, String content) {
		if (StringUtil.isEmpty(phones)) return false;
		//没用上群发机制，故注释掉
		//if (phones.split(",").length >1) return sendMultiSMS(phones, content);
		return sendSingleSMS(phones, content);
	}
	
	@Override
	public boolean sendSingleSMS(String phone, String content) {
		if (StringUtil.isEmpty(phone) || StringUtil.isEmpty(content)) return false;

		boolean success = false;
		try {
			String url=Props.getValue("Configs.SMS.URL");
			String key=Props.getValue("Configs.SMS.KEY");
			String uname=Props.getValue("Configs.SMS.UNAME");
			key = new MD5().getMD5ofStr(key).toUpperCase();

			log.info("给 {} 发短信，短信内容：{}", phone, content);
			String param =
				"<Group Login_Name=\"{0}\" Login_Pwd=\"{1}\" OpKind=\"0\" InterFaceID=\"\" SerType=\"短信\">{2}</Group>";
			String item = "<E_Time></E_Time>"
				+ "<Item><Task>"
				+ "<Recive_Phone_Number>{0}</Recive_Phone_Number>"
				+ "<Content><![CDATA[{1}]]></Content>"
				+ "<Search_ID>{2}</Search_ID>"
				+ "</Task></Item>";

			item = MessageFormat.format(item, phone, content, phone);
			param = MessageFormat.format(param, uname, key, item);
			String responseString = HttpClientUtil.httpPostContent(url, param, "gbk", "application/xml");
			log.debug(responseString);
			if ("00".equals(responseString)) {
				success = true;
				Redis.c.incr(Redis.K.SMS_SUCCESS.me());
				log.info("{}，短信发送成功！", phone);
			} else {
				success = false;
				Redis.c.incr(Redis.K.SMS_ERROR.me());
				log.error("{}，短信发送失败：{}", phone, responseString);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return success;
	}

}
